<?php
/**
 * Introduction: 秒杀商品表  product_spike
 */

namespace Common\Model;


use Think\Model;

class ProductSpikeModel extends Model
{
    //定义数据库连接信息
    protected $connection;
    //定义模型字段

    //定义表名
    protected $tableName = 'product_spike';

    /**
     * 初始化
     */
    public function _initialize()
    {

        $this->connection = C('Databaese');

    }

    /**
     * 获取商品列表
     * @param array $ids
     */
    public function getList()
    {
        //获取热卖商品ID
        $fields = [
            'ps.pid',
            'ps.sku_id',
            'ps.sec_price',
            'ps.start_time',
            'ps.end_time',
            'ps.sec_total',
            'ps.sec_true_stock',
            'ps.describe',
            'ps.pic',
            'pk.pname',
            'pk.guojia',
            'pk.guojiapic',
            'pk.market_price',
            'pk.ms_stock_supplierid',
            'pk.ms_entrepot_supplierid',
            'pk.ms_direct_supplierid'
        ];
        $list = $this->alias('ps')->join("left join haiou_product_sku as pk on ps.sku_id=pk.id ")->where("ps.status=1")->field($fields)->select();
        if (empty($list)) {
            $list = [];
        }
        $model = new ProductSkuModel();
        foreach($list as $k=>$v){
            $ways = [];
            $way = $model->getShipmentsWay($v['sku_id'], 1, $v['ms_stock_supplierid']);
            if (!empty($way)) {
                $ways[] = $way;
            }

            //是否存在于保税仓库
            $warehouseType = 1;
            $way = $model->getShipmentsWay($v['sku_id'], 2, $v['ms_entrepot_supplierid']);
            if (!empty($way)) {
                $ways[] = $way;
            }

            //是否存在于直邮仓
            $warehouseType = 1;
            $way = $model->getShipmentsWay($v['sku_id'], 3, $v['ms_direct_supplierid']);
            if (!empty($way)) {
                $ways[] = $way;
            }
            //$pic = explode(',',$v['pic']);
            //$list[$k]['pic'] = $pic[0];
            $list[$k]['supplierId'] = $ways[0]['supplierId'];
            $list[$k]['stock'] = $ways[0]['stock'];
            $list[$k]['warehouseType'] = $ways[0]['warehouseType'];
        }
        return $list;
    }

    public function getOne($id){
        //获取热卖商品ID
        $fields = [
            'ps.pid',
            'ps.sku_id',
            'ps.sec_price',
            'ps.start_time',
            'ps.end_time',
            'ps.sec_total',
            'ps.sec_true_stock',
            'ps.describe',
            'ps.pic',
            'pk.pname',
            'pk.guojia',
            'pk.guojiapic',
            'pk.market_price',
            'pk.ms_stock_supplierid',
            'pk.ms_entrepot_supplierid',
            'pk.ms_direct_supplierid',
            'pk.video',
            'pk.sale',
            'pk.thing_num'
        ];
        $info = $this->alias('ps')->join("left join haiou_product_sku as pk on ps.sku_id=pk.id ")->where("ps.status=1 and ps.sku_id=$id")->field($fields)->find();

        $model = new ProductSkuModel();
        $ways = [];
        $way = $model->getShipmentsWay($info['sku_id'], 1, $info['ms_stock_supplierid']);
        if (!empty($way)) {
            $ways[] = $way;
        }

        //是否存在于保税仓库
        $warehouseType = 1;
        $way = $model->getShipmentsWay($info['sku_id'], 2, $info['ms_entrepot_supplierid']);
        if (!empty($way)) {
            $ways[] = $way;
        }

        //是否存在于直邮仓
        $warehouseType = 1;
        $way = $model->getShipmentsWay($info['sku_id'], 3, $info['ms_direct_supplierid']);
        if (!empty($way)) {
            $ways[] = $way;
        }
            //$pic = explode(',',$v['pic']);
            //$list[$k]['pic'] = $pic[0];
        if($info){
            $info['supplierId'] = $ways[0]['supplierId'];
            $info['stock'] = $ways[0]['stock'];
            $info['warehouseType'] = $ways[0]['warehouseType'];
        }
        return $info;
    }
}